package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.bo.PageInfo;
import com.yixin.hospital.entity.dto.req.guidance.GuidanceAddReqDTO;
import com.yixin.hospital.entity.dto.req.visit.VisitStopReqDTO;
import com.yixin.hospital.entity.vo.ListGuidanceVO;
import com.yixin.hospital.service.GuidanceService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/guidance")
@Api(value = "导诊管理", tags = "导诊管理")
@Slf4j
public class GuidanceController {

    @Resource
    private  GuidanceService guidanceService;

    @GetMapping("/list")
    @ApiOperation(value = "导诊列表")
    public ApiResult<PageInfo<ListGuidanceVO>> list(@RequestParam int currentPage, @RequestParam int pageSize,
                             @RequestParam(value = "name", required = false) String name,
                             @RequestParam(value = "deptId", required = true) Long deptId,
                             @RequestParam(value = "workAddressId", required = false) Long workAddressId,
                             @RequestParam(value = "jobOfficeId", required = false) Long jobOfficeId,
                             @RequestParam(value = "status", required = false) Integer status) {
        return guidanceService.allList(currentPage, pageSize, name, deptId, workAddressId, jobOfficeId, status);
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "添加/编辑")
    public ApiResult<?> add(@Valid @RequestBody GuidanceAddReqDTO guidanceAddReqDTO, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return guidanceService.addOrUpdate(guidanceAddReqDTO, userId, userName);
    }

    @GetMapping("/guidance-waiting-number")
    @ApiOperation(value = "部门科室的候诊人数和就诊人数")
    public ApiResult<?> list(@RequestParam(value = "deptId", required = true) Long deptId,
                             @RequestParam(value = "workAddressId", required = false) Long workAddressId,
                             @RequestParam(value = "jobOfficeId", required = false) Long jobOfficeId) {
        return guidanceService.guidanceWaitingNumber(deptId, workAddressId, jobOfficeId);
    }

    @PostMapping("/guidance-stop")
    @ApiOperation(value = "终止接口")
    @Transactional
    public ApiResult<?> visitStop(@Valid @RequestBody VisitStopReqDTO visitStopReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return guidanceService.guidanceStop(visitStopReqDTO, userName);
    }

    @GetMapping("/job-status-list")
    @ApiOperation(value = "部门的各状态就诊人数")
    public ApiResult<?> jobStatusList(@RequestParam(value = "deptId") Long deptId) {
        return guidanceService.jobStatusList(deptId);
    }
}
